<template>
    <view class="list">
        <radio-group class="radio-group" @change.stop.prevent="radioChange">
            <view class="item" v-for="(item, index) in allAddress" :key="index">
                <radio :value="item.id+''" :checked="item.isfirst+'' == '1'" color="#1890ff"/>

                <view class="right" @tap.stop.prevent="addto" :data-id="item.id+''">
                    <text class="h1">{{ item.name }}</text>
                    <text class="p">{{ item.address }}</text>
                    <text class="jump">〉</text>
                </view>
            </view>
        </radio-group>
        <view class="item addto" @tap="addrss">
            <image src="/static/images/add.png" />
            <text class="lable">添加新地址</text>
            <text class="jump">〉</text>
        </view>
    </view>
</template>

<script>
var config = require('../../config.js');
var http = require('../../utils/httpHelper.js');
var app = getApp();
export default {
    data() {
        return {
            userInfo: {},
            allAddress: [] //地址列表
        };
    },
    onLoad: function () {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        that.setData({
            userInfo: userInfo
        });
    },
    onShow: function () {
        this.getAllAddressList();
    },
    methods: {
        getAllAddressList: function () {
            var that = this;
            var data = {
                userId: this.userInfo.userId
            };
            http.httpGet(config.clubApi.getAddressList, data, function (res) {
                if (res.code == '200') {
                    that.setData({
                        allAddress: res.data
                    });
                }
            });
        },

        radioChange: function (e) {
            var id = e.detail.value;
            var data = {
                userId: this.userInfo.userId,
                id: id,
                isfirst: 1
            };
            http.httpPut(config.clubApi.editAddress, data, function (res) {
                if (res.code == '200') {
                    console.log('设置默认地址成功');
                } else {
                    console.log('设置默认地址失败');
                }
            });
        },

        addrss: function (e) {
            uni.navigateTo({
                url: '/pages/address/addto/index?id='
            });
        },

        addto: function (e) {
            var id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '/pages/address/addto/index?id=' + id
            });
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
